package collection.queue.block_queue;

import org.junit.Test;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2019/4/16 5:32 PM
 */
public class LinkedBlockingQueueTest {

    /**
     * @see LinkedBlockingQueue 是一种阻塞队列，
     */
    @Test
    public void test1() {

        // 不指定长度
        LinkedBlockingQueue queue = new LinkedBlockingQueue();

        // 指定长度
        LinkedBlockingQueue queueq16 = new LinkedBlockingQueue(16);
    }

    @Test
    public void test2() {
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue(2);

        // offer 方法 和 add 方法 都可以添加
        queue.offer("111");
        queue.add("222");

        // 使用 offer 方法，若阻塞队列已满，则添加失败，返回 false
        boolean o3 = queue.offer("333");
        System.out.println(o3);

        // 使用 add 方法，若阻塞队列已满，则添加失败，抛出异常 java.lang.IllegalStateException: Queue full
        boolean a = queue.add("333");

    }

    @Test
    public void test3() throws InterruptedException {
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue(2);

        queue.put("111");
        queue.put("222");

        // 若队列已满，则线程进行阻塞
        queue.put("333");

    }

    @Test
    public void test4() throws InterruptedException {
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue(10);


        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    // put 放入队列，队列已满时线程阻塞
                    queue.put("--->" + i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();

        Thread thread2 = new Thread(() -> {
            while (true) {
                try {
                    // take 取出节点，队列已满时线程阻塞
                    String node = queue.take();
                    System.out.println(node);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread2.start();
        thread2.join();
    }

    @Test
    public void test5() throws InterruptedException {
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue(10);
        queue.put("111");
        queue.put("222");
        queue.put("333");

        // 取出队列首部的值，并将节点移除队列
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }

    @Test
    public void test6() throws InterruptedException {
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue(10);
        queue.put("111");
        queue.put("222");
        queue.put("333");

        // 不将节点移除队列
        System.out.println(queue.peek());
        System.out.println(queue.peek());
        System.out.println(queue.peek());
        System.out.println(queue.peek());
    }

    @Test
    public void test7() throws InterruptedException {

    }


}

